package com.chuangke.system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.system.dao.mapper.SysFocusMapper;
import com.chuangke.system.entity.SysFocus;
import com.chuangke.system.service.SysFocusService;

import cn.hutool.core.util.StrUtil;

@Service
public class SysFocusServiceImpl implements SysFocusService {

	@Autowired
	private SysFocusMapper sysFocusMapper;

	@Override
	public void focus(SysFocus sysFocus) {
		if(!isFocus(sysFocus.getUserId(), sysFocus.getPropId())){
			sysFocusMapper.insert(sysFocus);
		}
	}

	@Override
	public void cancel(String userId, String propId) {
		sysFocusMapper.delete(
				new QueryWrapper<SysFocus>().lambda().eq(SysFocus::getUserId, userId).eq(SysFocus::getPropId, propId));
	}

	@Override
	public boolean isFocus(String userId, String propId) {
		long count = sysFocusMapper.selectCount(new QueryWrapper<SysFocus>().lambda().eq(SysFocus::getUserId, userId).eq(SysFocus::getPropId, propId)) ;
		return count>=1;
	}
	
	@Override
	public IPage<SysFocus> page(CkPageQuery pageQuery) {
		String propType = (String) pageQuery.get("type");
		QueryWrapper<SysFocus> queryWrapper = new QueryWrapper<>();
		if (!StrUtil.isBlank(propType)) {
			queryWrapper.lambda().eq(SysFocus::getPropType, propType);
		}
		return sysFocusMapper.selectPage(pageQuery.getPage(), queryWrapper);
	}


}
